<template>
<!--商品管理组件-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="menuName">
        <el-input
          v-model="queryParams.searchTitle"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="commodityList"
      row-key="commodityId">

      <el-table-column label="商品ID" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="title" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            :size="50"
            :src="scope.row.cover"
            :key="scope.row.id">
            <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.price }} &nbsp;元</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="商品库存" align="center" prop="amount" />
      <el-table-column label="商品状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="updateStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-info" @click="commodityDetail(scope.row)">详情</el-button>

          <el-button size="small" type="text" icon="el-icon-edit" @click="commodityEdit(scope.row)">编辑</el-button>

          <el-button size="small" type="text" icon="el-icon-error" @click="commodityDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      :title="title"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose">
      <span>我来啦!</span>
    </el-drawer>
  </div>
</template>

<script>
import {deleteCommodity, listCommodity, updateCommodityStatus} from "@/api/pay/paylist";

export default {
  dicts: ['sys_commodity_status'],
  data() {
    return {
      // 抽屉
      drawer: false,
      title: "",
      queryParams:{
        searchTitle:"",
        pageNum:0,
        pageSize:10
      },
      commodityList: [],
      total:0,    // 数据总数
      loading: true,     // 遮罩层
      showSearch: true,  // 显示搜索
    }
  },
  methods: {
    // 搜索
    handleQuery(){
      this.getList()
      this.$message.success("搜索成功");
    },
    // 重置
    resetQuery(){
      this.queryParams.searchTitle = "";
      this.getList()
      this.$message.success("列表已重置");
    },
    // 获取列表
    getList(){
      listCommodity(this.queryParams).then(res=>{
        this.commodityList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },

    // 改变上下架状态
    updateStatus(row){
      console.log(row.id, row.status)
      updateCommodityStatus(row.id, row.status).then(res=>{
        let status = row.status ===  1 ? "上架" : "下架";
        this.$message.success(  `应用${status}成功`);
        this.getList();
      })
    },
    commodityDetail(){
      this.drawer = true;
      this.title = "商品详情"
    },
    commodityEdit(){
      this.drawer = true;
      this.title = "商品编辑"
    },

    commodityDelete(row){
      this.$confirm('确认删除当前商品？').then(() => {
        deleteCommodity(row.id).then(res=>{
          this.$message.success("删除成功");
          this.getList();
        })
      }).catch(_ => {});
    },

    // 抽屉关闭确认
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    }
  },
  mounted() {
    this.getList();
  }
}
</script>

<style scoped>

</style>
